package cn.green.core.components.dialect.impls.mysql.dialects;

import cn.green.core.components.dialect.DatabaseDclDialect;
import cn.green.core.components.dialect.impls.mysql.grammar.MysqlGrammars;
import cn.green.core.components.entity.DatabaseSql;
import cn.green.core.components.exceptions.QDclException;
import cn.green.core.model.QField;
import cn.green.core.model.QTable;

/**
 * @author wang.yishu
 * @version 1.0
 * @className MysqlDclDialect
 * @description mysql的数据库控制方言
 * @date 2024/11/22 13:46
 */
//Done
public class MysqlDclDialect implements DatabaseDclDialect {
    @Override
    public DatabaseSql existTable(QTable table) {
        if (table == null) {
            throw new QDclException("mysql dcl language executes error : table structure is null.");
        }
        String schemeName = table.getSchemeName();
        String tableName = table.getTableName();

        if (tableName == null || "".equals(tableName.trim())) {
            throw new QDclException("mysql dcl language executes error : table name is empty.");
        }
        String sql = MysqlGrammars.DQL.SELECT
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.COUNT
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.FROM
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.INFORMATION_SCHEMA_TABLE
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.WHERE
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.TABLE_SCHEMA
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.EQ
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + schemeName
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.AND
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.TABLE_NAME
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.EQ
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + tableName
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + MysqlGrammars.SYMBOL.SEMICOLON;
        return new DatabaseSql(sql, null);
    }

    @Override
    public DatabaseSql existField(QField field) {
        if (field == null) {
            throw new QDclException("mysql dcl language executes error : field structure is null.");
        }
        String schemeName = field.getSchemeName();
        String tableName = field.getTableName();
        String fieldName = field.getFieldName();
        if (tableName == null || "".equals(tableName.trim())) {
            throw new QDclException("mysql dcl language executes error : table name is empty.");
        }
        if (fieldName == null || "".equals(fieldName.trim())) {
            throw new QDclException("mysql dcl language executes error : field name is empty.");
        }
        String sql = MysqlGrammars.DQL.SELECT
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.COUNT
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.FROM
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.INFORMATION_SCHEMA_COLUMN
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.WHERE
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.TABLE_SCHEMA
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.EQ
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + schemeName
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.AND
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.TABLE_NAME
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.EQ
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + tableName
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.AND
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.DCL.COLUMN_NAME
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.CONDITION.EQ
                + MysqlGrammars.SYMBOL.BLANKET
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + fieldName
                + MysqlGrammars.SYMBOL.S_QT_MARK
                + MysqlGrammars.SYMBOL.SEMICOLON;
        return new DatabaseSql(sql, null);
    }
}
